#include "configmanager.h"

ConfigManager& ConfigManager::instance()
{
    static ConfigManager instance;
    return instance;
}

ConfigManager::ConfigManager(QObject *parent)
    : QObject(parent),
    m_settings("qmcc_ics_config.ini", QSettings::IniFormat)
{
    // 设置网络配置默认值
    if(!m_settings.contains("Network/ICSTcpPort")) {
        m_settings.setValue("Network/ICSTcpPort", 11003);
    }
    if(!m_settings.contains("Network/ICSCameraUdpPort")) {
        m_settings.setValue("Network/ICSCameraUdpPort", 11008);
    }
    if(!m_settings.contains("Network/ICSMcuUdpPort")) {
        m_settings.setValue("Network/ICSMcuUdpPort", 11000);
    }
    if(!m_settings.contains("Network/SlavePort")) {
        m_settings.setValue("Network/SlavePort", 10001);
    }

    // 设置从板卡IP默认值
    for(int i = 0; i < 4; ++i) {
        if(!m_settings.contains(QString("Network/Slave%1IP").arg(i))) {
            m_settings.setValue(QString("Network/Slave%1IP").arg(i),
                                QString("192.168.0.%1").arg(101 + i));
        }
    }
}

// 完整实现所有getter方法
QString ConfigManager::cameraIP() const {
    return m_settings.value("Network/CameraIP").toString();
}

quint16 ConfigManager::cameraPort() const {
    return m_settings.value("Network/CameraPort").toUInt();
}

QString ConfigManager::mcuIP() const {
    return m_settings.value("Network/MCUIP").toString();
}

quint16 ConfigManager::mcuPort() const {
    return m_settings.value("Network/MCUPort").toUInt();
}

quint16 ConfigManager::icstcpPort() const {
    return m_settings.value("Network/ICSTcpPort", 9076).toUInt();
}

quint16 ConfigManager::icsCameraUdpPort() const {
    return m_settings.value("Network/ICSCameraUdpPort", 11008).toUInt();
}

quint16 ConfigManager::icsMcuUdpPort() const {
    return m_settings.value("Network/ICSMcuUdpPort", 11000).toUInt();
}

QString ConfigManager::slaveIp(int index) const {
    return m_settings.value(QString("Network/Slave%1IP").arg(index)).toString();
}

quint16 ConfigManager::slavePort() const {
    return m_settings.value("Network/SlavePort", 10001).toUInt();
}

QString ConfigManager::binPath() const {
    return m_settings.value("Paths/BinPath").toString();
}

QString ConfigManager::fitsPath() const {
    return m_settings.value("Paths/FitsPath").toString();
}

QString ConfigManager::mountPath() const {
    return m_settings.value("Paths/MountPath").toString();
}

QString ConfigManager::logPath() const {
    return m_settings.value("Paths/LogPath").toString();
}

double ConfigManager::ccdTargetTemp() const {
    return m_settings.value("Temperature/CCDTargetTemp").toDouble();
}

double ConfigManager::coolerTargetTemp() const {
    return m_settings.value("Temperature/CCDTargetTemp").toDouble();
}

double ConfigManager::coolerTargetVoltage() const {
    return m_settings.value("Temperature/CCDTargetTemp").toDouble();
}

double ConfigManager::ccd1MaxTemp() const {
    return m_settings.value("TemperatureAlarm/CCD1_Max", 30.0).toDouble();
}

double ConfigManager::ccd2MaxTemp() const {
    return m_settings.value("TemperatureAlarm/CCD2_Max", 30.0).toDouble();
}

double ConfigManager::ccd3MaxTemp() const {
    return m_settings.value("TemperatureAlarm/CCD3_Max", 30.0).toDouble();
}

double ConfigManager::ccd4MaxTemp() const {
    return m_settings.value("TemperatureAlarm/CCD4_Max", 30.0).toDouble();
}

double ConfigManager::base1MaxTemp() const {
    return m_settings.value("TemperatureAlarm/Base1_Max", 30.0).toDouble();
}

double ConfigManager::base2MaxTemp() const {
    return m_settings.value("TemperatureAlarm/Base2_Max", 30.0).toDouble();
}

double ConfigManager::absorbColdMaxTemp() const {
    return m_settings.value("TemperatureAlarm/AbsorbCold_Max", 30.0).toDouble();
}

double ConfigManager::circuitMaxTemp() const {
    return m_settings.value("TemperatureAlarm/Circuit_Max", 30.0).toDouble();
}

double ConfigManager::dewarMaxTemp() const {
    return m_settings.value("TemperatureAlarm/Dewar_Max", 30.0).toDouble();
}

double ConfigManager::backup1MaxTemp() const {
    return m_settings.value("TemperatureAlarm/Backup1_Max", 30.0).toDouble();
}

double ConfigManager::backup2MaxTemp() const {
    return m_settings.value("TemperatureAlarm/Backup2_Max", 30.0).toDouble();
}
double ConfigManager::outputMax() const {
    return m_settings.value("TemperatureAlarm/Output_Max", 30.0).toDouble();
}

double ConfigManager::ccd1MinTemp() const {
    return m_settings.value("TemperatureAlarm/CCD1_Min", 30.0).toDouble();
}

double ConfigManager::ccd2MinTemp() const {
    return m_settings.value("TemperatureAlarm/CCD2_Min", 30.0).toDouble();
}

double ConfigManager::ccd3MinTemp() const {
    return m_settings.value("TemperatureAlarm/CCD3_Min", 30.0).toDouble();
}

double ConfigManager::ccd4MinTemp() const {
    return m_settings.value("TemperatureAlarm/CCD4_Min", 30.0).toDouble();
}

double ConfigManager::base1MinTemp() const {
    return m_settings.value("TemperatureAlarm/Base1_Min", 30.0).toDouble();
}

double ConfigManager::base2MinTemp() const {
    return m_settings.value("TemperatureAlarm/Base2_Min", 30.0).toDouble();
}

double ConfigManager::absorbColdMinTemp() const {
    return m_settings.value("TemperatureAlarm/AbsorbCold_Min", 30.0).toDouble();
}

double ConfigManager::circuitMinTemp() const {
    return m_settings.value("TemperatureAlarm/Circuit_Min", 30.0).toDouble();
}

double ConfigManager::dewarMinTemp() const {
    return m_settings.value("TemperatureAlarm/Dewar_Min", 30.0).toDouble();
}

double ConfigManager::backup1MinTemp() const {
    return m_settings.value("TemperatureAlarm/Backup1_Min", 30.0).toDouble();
}

double ConfigManager::backup2MinTemp() const {
    return m_settings.value("TemperatureAlarm/Backup2_Min", 30.0).toDouble();
}
double ConfigManager::outputMin() const {
    return m_settings.value("TemperatureAlarm/Output_Min", 30.0).toDouble();
}

double ConfigManager::vacuumMin() const {
    return m_settings.value("VacuumAlarm/Vacuum_Min", 30.0).toDouble();
}
double ConfigManager::vacuumMax() const {
    return m_settings.value("VacuumAlarm/Vacuum_Max", 30.0).toDouble();
}

double ConfigManager::currentMin() const {
    return m_settings.value("VacuumAlarm/Current_Min", 0.1).toDouble();
}

double ConfigManager::currentMax() const {
    return m_settings.value("VacuumAlarm/Current_Max", 5.0).toDouble();
}

int ConfigManager::alarmDelay() const {
    return m_settings.value("AlertServer/AlarmDelay", 10).toInt();
}

QString ConfigManager::alertServerUrl() const {
    return m_settings.value("AlertServer/Url").toString();
}

QString ConfigManager::fitsServerUrl() const {
    return m_settings.value("fitsServer/Url").toString();
}

QString ConfigManager::alertServerToken() const {
    return m_settings.value("AlertServer/Token").toString();
}

int ConfigManager::alertServerTimeout() const {
    return m_settings.value("AlertServer/Timeout", 5000).toInt();
}

void ConfigManager::setValue(const QString &group, const QString &key, const QVariant &value) {
    m_settings.beginGroup(group);
    m_settings.setValue(key, value);
    m_settings.endGroup();
    m_settings.sync();
}
